package com.ibm.law.ui.common;

import java.util.List;

import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.ibm.law.domain.Model;

public class CommonTableViewer extends TableViewer {
	private List<Model> list;
	private TableViewerColumn firstEditableViewerColumn;

	public CommonTableViewer(Model model, Composite parent, int style) {
		super(parent, style);
		createTable(model);
	}

	public CommonTableViewer(Model model, Table table) {
		super(table);
		createTable(model);
	}

	private void createTable(Model model) {
		this.model = model;
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				this) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};

		TableViewerEditor.create(this, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);
		createColumns();
	}

	private Model model;

	public CommonTableViewer(Model model, Composite parent) {
		super(parent);
		createTable(model);
	}

	private void createColumns() {
		List<String> columnHeaders = model.getColumnHeaderList();
		List<String> columnList = model.getColumnList();
		for (int i = 0; i < columnHeaders.size(); i++) {
			String header = columnHeaders.get(i);
			String column = columnList.get(i);
			TableViewerColumn tableViewerColumn = new TableViewerColumn(this,
					SWT.NONE);
			if (model.getEditors().get(column) != null
					&& firstEditableViewerColumn == null) {
				firstEditableViewerColumn = tableViewerColumn;
				firstEditableViewerColumn.getColumn().setData("index", i);
			}
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText(header);
			tblclmnNewColumn.setData("columnName", columnList.get(i));
			tableViewerColumn.setEditingSupport(new CommonEditingSupport(this,
					tableViewerColumn, model));
		}
	}

	public void load(List<Model> models) {
		this.list = models;
		this.setInput(models);
	}

	public Model getSelectedModel() {
		if (this.getSelection() != null) {
			IStructuredSelection selection = (IStructuredSelection) this
					.getSelection();
			return (Model) selection.getFirstElement();
		}
		return null;
	}

	public void add() {
		try {
			Model model = this.model.getClass().newInstance();
			this.list.add(model);
			this.refresh();
			firstEditableViewerColumn.getViewer().editElement(model,
					(Integer)firstEditableViewerColumn.getColumn().getData("index"));
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public void delete() {
		this.list.remove(getSelectedModel());
		this.refresh();
		this.getTable().setSelection(1);
	}
}
